// 四相曲线颜色 自定义 橙 绿 红
var fourPhaseColor = ['#2B73AF', '#FFA500', '#00FF00', '#FF0000'];
// 三相曲线颜色 橙 绿 红
var threePhaseColor = ['#FFA500', '#00FF00', '#FF0000'];
var otherColor = ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92']


$(function(){
    parent.block();
});

// 日期格式化方法，给日期补位
function date2Str(date){

    var year = date.getFullYear();
    var month = date.getMonth() + "";
    var day = date.getDate() + "";
    var hour = date.getHours() + "";
    var minute = date.getMinutes() + "";
    
    month = month.length == 1 ? "0" + (parseInt(month) + 1) : (parseInt(month) + 1);
    day = day.length == 1 ? "0" + day : day;
    hour = hour.length == 1 ? "0" + hour : hour;
    minute = minute.length == 1 ? "0" + minute : minute;
    
    return year + "-" + month + "-" +
    day +
    " " +
    hour +
    ":" +
    minute +
    ":00";
}

// 绘制曲线图
/**
 titleText：曲线图标题
 startTime：曲线的开始时间
 colors：颜色数组
 time：时间数组
 yAxisText：y轴文本
 maxVal：y轴的最大值
 minVal：y轴的最小值
 unit：单位
 seriesVal：数据，json格式
 */
function showChart(container, titleText, startTime, colors, time, yAxisText, maxVal, minVal, unit, seriesVal){

    // 应用HighChart的主题
    var highchartsOptions = Highcharts.setOptions(Highcharts.theme);
    
    //var container = window.frames["lineChart"].document.getElementById("container")
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: container,
            type: 'line',
            marginRight: 170,
            marginBottom: 25
        },
        title: {
            text: titleText,
            x: -20 //center
        },
        subtitle: {
            text: '起始时间：' + startTime,
            x: -20
        },
        credits: {
            href: "#",
            text: "wasion"
        },
        colors: colors,
        xAxis: {
            categories: time,
            tickInterval: 10,//间隔20
            labels: {
                formatter: function(){
                    var vDate = new Date(Date.parse(this.value.replace(/-/g, "/")));
                    return date2Str(vDate).substr(11);// 只显示时间
                }
            }
        },
        yAxis: {
            title: {
                text: yAxisText
            },
            max: maxVal,
            min: minVal,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function(){
                return '<b>' + this.series.name + '</b><br/>' +
                this.x +
                ' 值: ' +
                this.y +
                unit;
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: seriesVal
    });
    
    // 去掉遮盖层
    parent.unBlock();
}

// 修正YAxis坐标，确保最大值显示在partNum的位置，如2/3
function correctYAxisMax(arr, partNum){

    // 从数组中取出最大值
    var maxVal = -999999999;
    for (var i = 0; i < arr.length; i++) {
        maxVal = arr[i] > maxVal ? arr[i] : maxVal;
    }
    
    maxVal /= partNum;
    
    return maxVal;
}

// 修正YAxis坐标，确保最小值显示在partNum的位置，如2/3
function correctYAxisMin(arr, partNum){

    // 从数组中取出最小值
    var minVal = 0;
    for (var i = 0; i < arr.length; i++) {
        minVal = arr[i] < minVal ? arr[i] : minVal;
    }
    
    minVal /= partNum;
    
    return minVal;
}
